package com.tao.power.Controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.tao.power.Dao.LoginMapper;
import com.tao.power.Dto.Result;
import com.tao.power.Dto.RolerPowerDto;
import com.tao.power.Dto.RolerQueryDto;
import com.tao.power.Pojo.Roler;
import com.tao.power.Pojo.RolerPower;
import com.tao.power.Pojo.User;
import com.tao.power.Service.LoginService;
import com.tao.power.Service.RolerService;
import com.tao.power.config.AuthorityAnnotation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/roler")
public class RolerController {

    @Resource
    private RolerService rolerService;

    @Resource
    private LoginService loginService;

    @GetMapping("/queryRoler")
    public Result queryRoler(Integer count,Integer currentPage){
        return rolerService.queryRoler(count,currentPage);
    }

    @PostMapping("/deleteRoler")
    @AuthorityAnnotation(value = "deleteRoler")
    public Result deleteRoler(@RequestBody Integer id){
        return rolerService.deleteRoler(id);
    }

    @PostMapping("/updateRoler")
    @AuthorityAnnotation(value = "updateRoler")
    public Result updateRoler(@RequestBody Roler roler) throws Exception {
        return rolerService.updateRoler(roler);
    }

    @PostMapping("/insertRoler")
    @AuthorityAnnotation(value = "insertRoler")
    public Result insertRoler(@RequestBody Roler roler) throws Exception {
        return rolerService.insertRoler(roler);
    }

    @PostMapping("/queryRolerBySearch")
    @AuthorityAnnotation(value = "rolesBatchDelete")
    public Result queryRolerBySearch(@RequestBody RolerQueryDto rolerQueryDto){
        return rolerService.queryRolerBySearch( rolerQueryDto.getRolerName(),rolerQueryDto.getRolerStatus(),
                rolerQueryDto.getStartTime(), rolerQueryDto.getEndTime(),rolerQueryDto.getCurrentPage(),rolerQueryDto.getPageSize());
    }

    @PostMapping("/rolesBatchDelete")
    public Result batchDel(@RequestBody JSONArray idListArray){
        List<Integer> idList = JSONUtil.toList(idListArray,int.class).stream().distinct().collect(Collectors.toList());
        return rolerService.batchDel(idList);
    }

    @GetMapping("/queryRoles")
    public Result queryRoles(){
        return rolerService.queryRoles();
    }

    @PostMapping("/queryRolesById")
    public Result queryRolesById(@RequestBody Integer id){
        return rolerService.queryRolesById(id);
    }

    @PostMapping("/updateRoles")
    @AuthorityAnnotation(value = "updateRoles")
    public Result updateRoles(@RequestBody Map<String,Object> rolesInfo){
        List<Integer> rolesIds = (List<Integer>) rolesInfo.get("roles");
        Integer rolesId = (Integer) rolesInfo.get("id");

        return rolerService.updateRoles(rolesIds,rolesId);
    }

    @PostMapping("/updateStatus")
    @AuthorityAnnotation(value = "updateRoler")
    public Result updateStatus(@RequestBody Roler roler){
        return rolerService.updateStatus(roler);
    }

    @PostMapping("/getPower")
    public Result getPower(@RequestBody Integer id){
        List<String> power = loginService.queryPowerByUser(id);
        return Result.ok("",power,200);
    }

}
